package com.minglv.mini.service.impl;

import com.minglv.mini.domain.*;
import com.minglv.mini.mapper.*;
import com.minglv.mini.service.IGoodsService;
import com.minglv.mini.tools.JWTUtils;
import com.minglv.mini.tools.MiniResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//@DataSource(DataSourceType.SLAVE)
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    ActivityMapper activityMapper;

    @Resource
    UserFavoritesMapper userFavoritesMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    OrderMapper orderMapper;

    @Resource
    IntegralLogMapper integralLogMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    @Override
//    @DataSource(DataSourceType.SLAVE)
    public List<Map> getList(Integer type){
        return goodsMapper.getList(type);
    }

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Map iscollect(String token, Integer id) {

        Long userId = JWTUtils.getUserid(token);

        if (ObjectUtils.isEmpty(userId)) {
            return null;
        }else{
            Map<String,Object> re = new HashMap<>();
            List<Map> userFavorites = activityMapper.getUserFavorites(2,id, userId);
            if (CollectionUtils.isEmpty(userFavorites)) {

                UserFavorites favorites = new UserFavorites();
                Long maxId = userFavoritesMapper.getMaxId();
                favorites.setStatus(1);
                favorites.setId(maxId + 1);
                favorites.setItemId(Long.valueOf(id));
                favorites.setUserId(userId);
                favorites.setType(2);
                userFavoritesMapper.insertUserFavorites(favorites);
                re.put("user_id",userId);
                re.put("id",favorites.getId());
                re.put("item_id",favorites.getItemId());
                re.put("type",favorites.getType());
                re.put("status",favorites.getStatus());
                re.put("create_at",new Date());
                re.put("update_at",new Date());
                return re;
            }

            re = userFavorites.get(0);

            return re;
        }
    }

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Object collect(String token, Long id) {
        Long userId = JWTUtils.getUserid(token);
        if (ObjectUtils.isEmpty(userId)) {
            return null;
        }else{
            Map<String,Object> re = new HashMap<>();
            UserFavorites userFavorites = new UserFavorites();
            userFavorites.setUserId(userId);
            userFavorites.setItemId(id);
            userFavorites.setType(2); //商品
            List<UserFavorites> userFavorites1 = userFavoritesMapper.selectUserFavoritesList(userFavorites);
            if (CollectionUtils.isEmpty(userFavorites1)) {
                UserFavorites uf = new UserFavorites();
                Long maxId = userFavoritesMapper.getMaxId();
                uf.setId(maxId + 1);
                uf.setStatus(1);
                uf.setUserId(userId);
                uf.setItemId(id);
                userFavoritesMapper.insertUserFavorites(uf);
                return uf;
            }else{
                UserFavorites userFavorites2 = userFavorites1.get(0);
                userFavorites2.setStatus(userFavorites2.getStatus() == 1 ? 0 : 1);
                userFavoritesMapper.updateUserFavorites(userFavorites2);
                return userFavorites2;
            }
        }
    }

    /**
     * 兑换商品
     * @param token
     * @param id
     * @return
     */
    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Object postExchange(String token, Long id) {
        Long userId = JWTUtils.getUserid(token);
        if (ObjectUtils.isEmpty(userId)) {
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }else{
            //账户余额
            Goods goods = goodsMapper.selectGoodsById(id);
            if (ObjectUtils.isEmpty(goods)) {
                return new MiniResult(201,"数据不存在",new HashMap());
            }

            User user = userMapper.selectUserById(userId);
            //积分
            if (user.getIntegral().compareTo(goods.getPrice()) == -1) {
                return new MiniResult(201,"积分不足,兑换失败!",new HashMap());
            }

            Order order = new Order();
            int min = 1000; // 最小值
            int max = 9999; // 最大值
            int randomNum = (int) (Math.random() * (max - min + 1)) + min;
            Long maxId = orderMapper.getMaxId();
            order.setId(maxId + 1);
            order.setUserId(userId);
            order.setOrdersn(DateUtils.dateTimeNow() + randomNum);
            order.setGoodsId(id);
            order.setGoodsTitle(goods.getTitle());
            order.setPrice(goods.getPrice());

            user.setIntegral(user.getIntegral().subtract(goods.getPrice()));

            IntegralLog ll = new IntegralLog();
            Long maxId2 = integralLogMapper.getMaxId();
            ll.setId(maxId2 + 1);
            ll.setUserId(userId);
            ll.setIntegral(goods.getPrice());
            ll.setRemark("积分兑换活动消耗");
            ll.setType(2);
            ll.setFromUser(userId);
            ll.setActiveId(goods.getId());
            integralLogMapper.insertIntegralLog(ll);

            userMapper.updateUser(user);
            int row = orderMapper.insertOrder(order);

            if (row > 0) {
                return new MiniResult(200,"ok",new HashMap());
            }else{
                return new MiniResult(201,"",new HashMap());
            }
        }
    }


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Goods selectGoodsById(Long id)
    {
        return goodsMapper.selectGoodsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param goods 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
//    @DataSource(DataSourceType.SLAVE)
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param goods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertGoods(Goods goods)
    {
        return goodsMapper.insertGoods(goods);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param goods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateGoods(Goods goods)
    {
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        return goodsMapper.deleteGoodsById(id);
    }
}
